import { notification } from 'antd'
import { useEffect, useState } from 'react'
import { useDebounce } from 'ahooks'

export const CMTBox = ({ CMT_ADMIN, UserEvent, CMTEvent }) => {
    const [api, contextHolder] = notification.useNotification()
    const [SDK, setSDK] = useState()
    const [UserList, setUserList] = useState({})
    const throttledValue = useDebounce(UserList, { wait: 350 })

    useEffect(() => {
        const sdk = new window.CmtWsClientHelper()
        sdk.addCmtEventHandler(new window.CallbackHandler())
        // 初始化安全帽
        const InItCmtClientService = () => {
            const { user, password, host, serverPort } = CMT_ADMIN
            window.onopen = () => sdk.registerSip(user, password, host, serverPort)
            window.setConfig = () =>
                sdk.setServiceConfig(
                    'videoPlayUrl',
                    `http://${host}:1080/vdis/videoPlayer?user=${user}&passwd=${password}`,
                )
            window.location = 'CmtClientService://'
            sdk.init()
            setSDK(sdk)
        }
        // 卸载安全帽
        const UnInItCmtClientService = () => {
            sdk.closeService()
            sdk.WSClose()
            window.onopen = () => null
            window.setConfig = () => null
        }
        InItCmtClientService()
        window.onbeforeunload = () => UnInItCmtClientService()
        return () => {
            UnInItCmtClientService()
        }
    }, [CMT_ADMIN])

    useEffect(() => {
        window.onEventFiredEvent = e => {
            const { event, code } = e
            switch (event) {
                case 'onOpen':
                    window.onopen()
                    break
                case 'onEventRegistered':
                    if (Number(code) === 200) {
                        api.success({ key: 'cmt', message: '指挥中心登录成功' })
                        CMTEvent.emit(SDK)
                        window.setConfig()
                    } else api.error({ key: 'cmt', message: '指挥中心登录失败!' })
                    break
                case 'onClose':
                    SDK.WSClose()
                    SDK.init()
                    api.info({ key: 'cmt', message: '指挥中心等待初始化...!' })
                    break
                case 'onEventUserStatusUpdate':
                    setUserList(Update => ({ ...Update, [e.sipNum]: e.status }))
                    break
                case 'onEventConnected':
                    e.type === 'audio' && console.log('语音事件', e)
                    e.type === 'video' && console.log('视频事件', e)
                    break
                default:
                    break
            }
        }
        return () => {
            window.onEventFiredEvent = () => {}
            window.onCmdResultEvent = () => {}
        }
    }, [CMTEvent, SDK, api])

    useEffect(() => {
        UserEvent.emit(throttledValue)
    }, [throttledValue, UserEvent])

    CMTEvent.useSubscription(val => SDK.makeVideoCall(val, 0, 0))

    return <> {contextHolder} </>
}
